from flask import Flask, render_template, request, jsonify
from switch_client import HuaweiSwitchClient
import logging

# 登录凭据
DEFAULT_PORT = '2222'
DEFAULT_USERNAME = 'ck'
DEFAULT_PASSWORD = 'ROOTiris##2024'

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/api/get_mac_bindings', methods=['POST'])
def get_mac_bindings():
    data = request.json
    try:
        client = HuaweiSwitchClient(
            host=data['host'],
            # port=int(data['port']),
            # username=data['username'],
            # password=data['password']
            port=DEFAULT_PORT,  # ★写死
            username=DEFAULT_USERNAME,  # ★写死
            password=DEFAULT_PASSWORD   # ★写死
        )
        result = client.get_bindings(data['subnet'])   # ★传网段
        return jsonify({'success': True, 'data': result})
    except Exception as e:
        logging.exception("获取绑定信息失败")
        return jsonify({'success': False, 'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)